package DSPackage;

import jade.core.*;
import jade.core.behaviours.*;

import jade.lang.acl.*;

import jade.content.*;
import jade.content.onto.basic.*;
import jade.content.lang.sl.*;

import jade.util.leap.Iterator;

import jade.domain.*;
import jade.domain.mobility.*;
import jade.domain.JADEAgentManagement.*;
import jade.domain.FIPAAgentManagement.*;

import java.util.*;
import java.io.*;

import jade.Boot;
import jade.core.*;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.*;

/**
 *
 */
public class DSAgent extends Agent {
    private boolean firstrun = true;
    private Map locations;
    private String appName;
    private AID replyAID;
    private AID[] agentID = null;
    private String homeContainer;
    private String convID;

    @Override
    protected void setup() {
        Object[] args = getArguments();
        this.locations = (HashMap) args[0];
        this.appName = (String) args[1];
        this.replyAID = (AID) args[2];
        this.agentID = (AID[]) args[3];
        this.homeContainer = (String) args[4];
        this.convID = (String) args[5];

        init();

        addBehaviour(new MoveAgent(this));

    }

    @Override
    protected void beforeMove() {

    }

    @Override
    protected void afterMove() {
        init();
    }

    private void init() {
        // Register SLCodec and MobilityOntology in content manager
        getContentManager().registerLanguage(new SLCodec());
        getContentManager().registerOntology(MobilityOntology.getInstance());
    }

class MoveAgent extends SimpleBehaviour {
    private boolean finished = false;
    private int[] acqBW = new int[agentID.length];
    private int maxBW = 0;
    private int index = 0;

    private static final String extension = ":1099/JADE";

    public MoveAgent(Agent a) {
        super(a);
    }



    @Override
    public void action() {
        Location loc = (Location) locations.get(index);
        index++;

        if (loc != null) doMove(loc);

        for(int i = 0; i<agentID.length; i++) {
            if (agentID[i].getName().equals(here().toString() + extension)
                    && !homeContainer.equals(here().getName())) {

                ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
                msg.setContent(appName);
                msg.addReceiver(agentID[i]);
                send(msg);

            }
            MessageTemplate mt = MessageTemplate.and(
                MessageTemplate.MatchSender(agentID[i]),
                MessageTemplate.MatchPerformative(ACLMessage.INFORM));
            ACLMessage reply = new ACLMessage(ACLMessage.INFORM);
            reply = receive(mt);

            if(reply != null) System.err.println(reply.toString());

            if(reply != null) {
                String temp = reply.getContent();
                acqBW[i] = Integer.parseInt(temp);
                System.err.println("acqbw: " + acqBW[i]);
            }
        }

        if(index >= (locations.size())){
            for(int i=0 ; i < agentID.length ; i++) {
                maxBW = (maxBW < acqBW[i]) ? acqBW[i] : maxBW;
            }

            ACLMessage msgClient = new ACLMessage(ACLMessage.INFORM);
            msgClient.setContent("" + maxBW);
            msgClient.setConversationId(convID);
            msgClient.addReceiver(replyAID);
            send(msgClient);

            finished = true;
        }

        block();

    }

    @Override
    public boolean done() {
        return finished;
    }
}
}
